package com.lagou.worker.service.impl;

import com.lagou.worker.api.MailService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

/**
 * @ClassName MailServiceImpl
 * @Description TODO
 * @Author xsq
 * @Date 2020/8/17 15:28
 **/
@Service
public class MailServiceImpl implements MailService {

    @Value("${spring.mail.username}")
    private String from;
    @Autowired
    private JavaMailSender mailSender;

    /**
     * 验证码发送到邮箱
     *
     * @param email
     * @param code
     */
    @Override
    public Boolean sendEmail(String email, String code) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(email);
        message.setSubject("验证码");
        message.setText("您的注册验证码为【" + code + "】");
        message.setFrom(from);
        try {
            mailSender.send(message);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
